<template>
  <div class="mk-products">
    <div class="swiper-container swiper1">
      <div class="swiper-wrapper">
        <div class="swiper-slide" v-for="(item) in bannerLists">
          <img :src="item.banner_pic" alt>
        </div>
      </div>
      <div class="swiper-pagination"></div>
    </div>
    <div class="bread-nav">
      <div class="nav-box">
        <span>您的位置：</span>
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">网站首页</el-breadcrumb-item>
          <el-breadcrumb-item>产品展示</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
    </div>
    <div class="content">
      <h3 class="title">产品展示</h3>
      <h4 class="sub-title">Products</h4>
      <div class="category">
        <div class="category-box">
          <div class="item" :class="current==-1?'active':''" @click="changeAllCategory()">全部</div>
          <div
            class="item"
            :class="current==index?'active':''"
            v-for="(item,index) in catogories"
            @click="changeCategory(index,item.id)"
          >{{item.cate_name}}</div>
        </div>
      </div>
      <div class="products" @mouseover="mouseover" ref="mouseEvent">
        <div class="pro-item" v-for="(item,index) in products.info" @click="goDetails(item.id)">
          <div class="pro-img">
            <img :src="'https://cdn1-open.mokyun.com/' + item.product_pic">
          </div>
          <h3 class="pro-title">{{item.product_name}}</h3>
        </div>
      </div>
      <div class="pages">
        <div class="pages-box" v-if="isAll">
          <div
            class="page-item"
            v-for="(item,index) in products.all_page"
            :class="activeClass==item?'active':''"
            @click="clickItem(item)"
          >{{item}}</div>
        </div>
        <div class="pages-box" v-else>
          <div
            class="page-item"
            v-for="(item,index) in products.all_page"
            :class="activeClass==item?'active':''"
            @click="clickItem1(item)"
          >{{item}}</div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import getData from "../api/getInfo";
export default {
  data() {
    return {
      bannerLists: [],
      products: {},
      activeClass: 1,
      catogories: [], //所有种类
      current: -1, //是否是当前项
      isAll: true, //是否是全部类目
      cate_id: "" //分类id
    };
  },
  mounted() {
    this.getProductsCategory();
    this.getBanner();
    this.getAllProducts(1);
  },
  methods: {
     initSwiper() {
      let mySwiper = new Swiper(".swiper1", {
        autoplay: {
          delay: 3000,
          disableOnInteraction: false
        },
        loop: true,
        pagination: {
          el: ".swiper-pagination",
          clickable: true,
           bulletActiveClass: "my-bullet-active"
        },
        observer: true
      });
    },
    mouseover(e) {
      let items = this.$refs.mouseEvent.querySelectorAll(".pro-item");
      for (var i = 0; i < items.length; i++) {
        // 进入
        items[i].addEventListener("mouseover", function() {
          this.style.boxShadow = " 0px 6px 16px 0px rgba(24, 29, 0, 0.15)";
          this.style.backgroundColor = "#96ce54";
          this.querySelector(".pro-title").style.color = "#fff";
        });
        // 离开
        items[i].addEventListener("mouseout", function() {
          this.style.boxShadow = "none";
          this.querySelector(".pro-title").style.color = "#323232";
          this.style.backgroundColor = "#fff";
        });
      }
    },
    //获取banner图
    getBanner() {
      getData
        .getBanner()
        .then(res => {
          let tempArr = [];
          res.data.forEach((val, index) => {
            if (val.type == 2) {
              val.banner_pic = `${getData.url}${val.banner_pic}`;
              tempArr.push(val);
            }
          });
          this.bannerLists = tempArr;
           this.$nextTick(() => {
            // 下一个UI帧再初始化swiper
            this.initSwiper();
          });
        })
        .catch(e => {
          console.log(e);
        });
    },
    //获取产品分类
    getProductsCategory() {
      getData
        .getProductsCategory()
        .then(res => {
          this.catogories = res.data;
        })
        .catch(e => {
          console.log(e);
        });
    },
    //获取所有产品
    getAllProducts(page) {
      getData
        .getAllProducts(page)
        .then(res => {
          this.products = res.data;
        })
        .catch(e => {
          console.log(e);
        });
    },
    //获取产品分类列表
    getCateProducts(cate_id, page_id) {
      getData
        .getCateProducts(cate_id, page_id)
        .then(res => {
          this.products = res.data;
        })
        .catch(e => {
          console.log(e);
        });
    },
    //全部类别产品分页
    clickItem(item) {
      this.activeClass = item;
      this.getAllProducts(item);
    },
    //单个分类产品分页
    clickItem1(item) {
      this.activeClass = item;
      this.getCateProducts(this.cate_id, item);
    },

    //全部产品点击事件
    changeAllCategory() {
      this.isAll = true;
      this.activeClass = 1;
      this.current = -1;
      this.getAllProducts(1);
    },

    //分类产品点击事件
    changeCategory(index, cate_id) {
      this.isAll = false;
      this.activeClass = 1;
      this.current = index;
      this.cate_id = cate_id;
      this.getCateProducts(cate_id, 1);
    },
    //跳转产品详情页
    goDetails(pro_id) {
      this.$router.push({
        path: `/productDetails/${pro_id}`
      });
    }
  }
};
</script>
<style scoped lang="scss">
.swiper-container {
  height: 505px;
  width: 100%;
}
/deep/.bread-nav {
  width: 100%;
  height: 52px;
  background: #f6f6f6;
  .nav-box {
    width: 1200px;
    height: 52px;
    line-height: 52px;
    margin: 0 auto;
    display: flex;
    span,
    a {
      font-size: 14px;
      color: #9d9d9d;
    }
    .current {
      color: #96ce54;
    }
    .el-breadcrumb {
      line-height: 52px;
    }
    .el-breadcrumb__inner {
      color: #96ce54;
    }
    .el-breadcrumb__inner.is-link {
      color: #9d9d9d;
      font-weight: normal;
    }
  }
}
.content {
  padding-bottom: 100px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  .title {
    font-size: 36px;
    color: #323232;
    margin: 76px 0 6px 0;
  }
  .sub-title {
    font-size: 18px;
    color: #b8b7b7;
  }
  .category {
    width: 100%;
    display: flex;
    justify-content: center;
    margin-top: 46px;
    margin-bottom: 77px;
    .category-box {
      display: flex;
      justify-content: space-around;
      .item {
        font-size: 16px;
        color: #999;
        padding: 0 40px;
        border-right: 1px solid #bbb;
        cursor: pointer;
      }
      .item:last-child {
        border-right: none;
      }
      .item.active {
        color: #323232;
      }
    }
  }
  .products {
    width: 990px;
    display: flex;
    flex-wrap: wrap;
    .pro-item {
      width: 302px;
      height: 372px;
      box-sizing: border-box;
      background: #fff;
      border: solid 4px #96ce54;
      margin-right: 42px;
      margin-bottom: 42px;
      display: flex;
      align-items: center;
      flex-direction: column;
      .pro-img {
        height: 254px;
        width: 294px;
        img {
          width: 100%;
          height: 100%;
        }
      }
      .pro-title {
        font-size: 22px;
        font-weight: normal;
        color: #323232;
        margin-top: 35px;
      }
    }
    .pro-item:nth-child(3n) {
      margin-right: 0;
    }
  }
  .pages {
    width: 100%;
    height: 50px;
    display: flex;
    justify-content: center;
    .pages-box {
      min-width: 100px;
      height: 50px;
      display: flex;
      .page-item {
        width: 50px;
        height: 50px;
        text-align: center;
        line-height: 50px;
        color: #323232;
        cursor: pointer;
      }
      .page-item.active {
        width: 50px;
        height: 50px;
        background-color: #96ce54;
        text-align: center;
        line-height: 50px;
        color: #fff;
      }
    }
  }
}
</style>


